package com.ngc.mall.controller;

import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.ArticleCategoryDomain;
import com.ngc.mall.domain.ArticleDomain;
import com.ngc.mall.service.IArticleCategoryService;
import com.ngc.mall.service.IArticleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * 内容
 * @author: chenxiaolin
 * @date 2023/9/16 15:16
**/
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService iArticleService;

    @Autowired
    private IArticleCategoryService iArticleCategoryService;

    @PostMapping("")
    @ApiOperation("新增文章")
    public ResponseResult<Long> create(@RequestBody ArticleDomain articleDomain){
        return ResponseResult.success(
                iArticleService.createArticle(articleDomain)
        );
    }

    @PostMapping("/category")
    @ApiOperation("新增文章分类")
    public ResponseResult<Long> createCategory(@RequestBody ArticleCategoryDomain articleCategoryDomain){
        return ResponseResult.success(iArticleCategoryService.createArticleCategory(articleCategoryDomain));
    }

    @PutMapping("/id/{id}")
    @ApiOperation("根据id更新文章")
    public ResponseResult<Boolean> updateById(@PathVariable Long id,@RequestBody ArticleDomain articleDomain){
        return ResponseResult.auto(iArticleService.updateById(id,articleDomain));
    }

    @GetMapping("/id/{id}")
    @ApiOperation("根据id获取文章")
    public ResponseResult<ArticleDomain> getById(@PathVariable Long id){
        return ResponseResult.success(iArticleService.getDetailById(id));
    }

    @GetMapping("/list")
    @ApiOperation("查看所有文章")
    public ResponseResult<List<ArticleDomain>> selectAll(){
        return ResponseResult.success(iArticleService.selectAll());
    }

    @GetMapping("/category/list")
    @ApiOperation("查看所有文章分类")
    public ResponseResult<List<ArticleCategoryDomain>> selectAllCategory(){
        return ResponseResult.success(iArticleCategoryService.selectAll());
    }

    @DeleteMapping("/id/{id}")
    @ApiOperation("根据id删除文章")
    public ResponseResult<Boolean> deleteById(@PathVariable Long id){
        return ResponseResult.auto(iArticleService.deleteById(id));
    }

    @DeleteMapping("/category/id/{id}")
    @ApiOperation("根据id删除文章分类")
    public ResponseResult<Boolean> deleteByCategoryId(@PathVariable Long id){
        return ResponseResult.auto(iArticleCategoryService.deleteById(id));
    }
    @PutMapping("/category/id/{id}")
    @ApiOperation("根据id更新文章分类")
    public ResponseResult<Boolean> updateByCategoryId(@PathVariable Long id,@RequestBody ArticleCategoryDomain domain){
        return ResponseResult.auto(iArticleCategoryService.updateById(id,domain));
    }
}
